package com.cai.kcsj.controller;

import com.cai.kcsj.entity.User;
import com.cai.kcsj.service.LoginServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @version 1.0
 * @author：蔡松坡
 * @date：2021/1/4 09 : 27
 * @package：com.cai.kcsj.controller
 */
@Controller
@RequestMapping("/login")
@CrossOrigin
public class LoginController {
    @Autowired
    private LoginServer loginServer;
    @RequestMapping("/login")
    public String login(Model model, User user){
        User login = loginServer.login(user);
        if(login!=null){
            if(login.getSf()==1){
                return "main";
            }else{
                model.addAttribute("sf","权限不足");
                return "redirect:/router/toLogin";
            }

        }
        model.addAttribute("loginmsg","用户不存在");
        return "redirect:/router/toLogin";
    }

//    @ResponseBody
    @RequestMapping("/regist")
    public String regist(Model model,User user){
        user.setSf(1);
        boolean regist = loginServer.regist(user);
        if(regist){
            return "login";
        }
        model.addAttribute("msg","注册失败");
        return "redirect:/login/regist";
    }
    @ResponseBody
    @RequestMapping("/ajaxreg")
    public String ajaxreg(@RequestParam("uname") String uname){

        int ajax = loginServer.ajax(uname);
        if(ajax>0){
            return "fail";
        }
        return "success";
    }


}
